package com.example.bloodbankproject;

import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.Spinner;

import com.example.bloodbankproject.global.GetResponse;
import com.example.bloodbankproject.global.Session;

public class SearchDonorActivity  extends Activity {

	private  ArrayList<String> newList;
	private ArrayList<String> donarList;   
	private Spinner mDgroup,mArea;
	private RelativeLayout mRelativeLayout;
	private Button mButton;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sreachdonor);
		
		mDgroup = (Spinner) findViewById(R.id.sp_group);
		mArea = (Spinner) findViewById(R.id.sp_area);
		mRelativeLayout = (RelativeLayout) findViewById(R.id.rr_layout);
		mButton =(Button) findViewById(R.id.getdonor);
		
		UrlConnection conn = new UrlConnection();
		String url = Session.sInstanceUrl+"/services/data/v26.0/query/?q=select+City__c+from+DonorRegistration__c";
		conn.execute(url);
	}
	
	class UrlConnection extends AsyncTask<String, Void, String> {

		private HttpURLConnection http_url_conn;
		private String response;
		@Override
		protected String doInBackground(String... params) {

			try {

				URL w_Url = new URL(params[0]);
				http_url_conn = (HttpURLConnection) w_Url.openConnection();
				http_url_conn.setRequestMethod("GET");
				// headers
				http_url_conn.setRequestProperty("Content-type", "application/json");
				http_url_conn.setRequestProperty("Authorization", "OAuth "+Session.sAccessToken);

				Log.i("NCS",
						"Response code is  :" + http_url_conn.getResponseCode());
				Log.i("NCS",
						"Response Method is  :"
								+ http_url_conn.getRequestMethod());
				Log.i("NCS",
						"Response Message is  :"    
								+ http_url_conn.getResponseMessage());

				response =GetResponse.convert_To_String(http_url_conn.getInputStream());

			

				Log.i("NCS", "Response is  :" +response);

				return response;

			} catch (Exception e) {
				e.printStackTrace();
			}
			return response;
		}

		@Override
		protected void onPostExecute(String result) {
			
			if(result!=null){
				
				
				try{
					
					donarList = new ArrayList<String>();
					
				 	JSONObject  _jObject = new JSONObject(response);
			 	   	
			 	   	JSONArray _jArray = _jObject.getJSONArray("records");
			 	   	
			 	   	int count = _jArray.length();
			 	   	
			 	   	for(int i=0;i<count;i++){
			 	   		
			 	   		
			 	   		String area = _jArray.getJSONObject(i).getString("City__c");
			 	   		
			 	   		donarList.add(area);
			 	   		
			 	   	
			 	   	}
			 	   	
			 	    newList = new ArrayList<String>(new HashSet<String>(donarList));
			 	   	refresh();
			 	   
			 	   	
				}catch (Exception e) {
					e.printStackTrace();
				}
				
			
			}
			super.onPostExecute(result);
		}


	}
	
	public void refresh(){
		
		mRelativeLayout.setVisibility(View.VISIBLE);
		mButton.setVisibility(View.VISIBLE);
		List<String> bloodBank = getBloodBank();

		ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(),R.layout.display, bloodBank);               

		mArea.setAdapter(adapter);
		
                
		ArrayList<String>  list = new ArrayList<String>();
		list.add("A Positive");
		list.add("A Negtive");
		list.add("B Positive");
		list.add("B Negtive");
		list.add("O Positive");
		list.add("O Negtive");
		list.add("AB Positive");
		list.add("AB Negtive");
		
		ArrayAdapter<String> bGroupAdapter = new ArrayAdapter<String>(getApplicationContext(),R.layout.display, list);               

		mDgroup.setAdapter(bGroupAdapter);
		
	}
	private List<String> getBloodBank() {

		List list = new ArrayList<String>();
		for (Object c : newList)    
		{
		list.add(c);
		}
		return list;
	}
	
	public void getDonor(View v) {
		
		String area = mArea.getSelectedItem().toString();

		String bloodgroup = mDgroup.getSelectedItem().toString();

		Intent i = new Intent(getApplicationContext(), GetDonorDetails.class);
		i.putExtra("K1", area);
		i.putExtra("K2", bloodgroup);
		startActivity(i);
	}
	
	
	
	
}